﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRS.Core.DataAccess;
using CRS.Core.Entities;
using VitCode.Web.UI.WebControls;

namespace CRS.Web.UserControls
{
    public partial class RoomCtl : System.Web.UI.UserControl
    {
        private const string ROOM_ID = "RoomId", ROOM_NAME = "RoomName",
            ROUND_ID="RoundId",ROOM_STATE = "RoomState";

        public string RoomId
        {
            get { return Convert.ToString(ViewState[ROOM_ID]); }
            set { ViewState[ROOM_ID] = value; }
        }
        public string RoundId
        {
            get { return Convert.ToString(ViewState[ROUND_ID]); }
            set { ViewState[ROUND_ID] = value; }
        }
        public string RoomName
        {
            get { return Convert.ToString(ViewState[ROOM_NAME]); }
            set { ViewState[ROOM_NAME] = value; }
        }
        public string RoomState
        {
            get { return Convert.ToString(ViewState[ROOM_STATE]); }
            set { ViewState[ROOM_STATE] = value; }
        }

        private void SetData()
        {
            if (!string.IsNullOrEmpty(RoomId) && !string.IsNullOrEmpty(RoomName) && !string.IsNullOrEmpty(RoomState))
            {
                lblName.Text = RoomName;
                var color = Color.LightGray;
                btnExce.Text = "开局";
                if (RoomState == RoomInfo.STATE_BOOKING)
                    color = Color.Yellow;
                else if (RoomState == RoomInfo.STATE_USING)
                {
                    color = Color.GreenYellow;
                    btnExce.Text = "结账";
                }
                lblName.BackColor = color;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                SetData();
            }
            
        }

        protected void btnBook_Click(object sender, EventArgs e)
        {
            var winBook = new Window(Page, "订房信息");
            winBook.Width = 420;
            winBook.Height = 300;
            winBook.ShowModalDialog("ReserveInfomation.aspx?RoomName=" + lblName.Text);
            var acc = new CoreDataAccess();
            try
            {
                acc.BeginTransaction();
                var obj = acc.GetSingleObjectById(typeof (RoomInfo).Name, RoomId);
                if(obj != null && obj is RoomInfo)
                {
                    var rooInfo = ((RoomInfo)obj);
                    rooInfo.State = RoomInfo.STATE_BOOKING;
                    acc.SaveOrUpdate(rooInfo);
                    var objRound = new RoundInfo();
                    objRound.Room = ((RoomInfo)obj);
                    objRound.StartDate = DateTime.Now;
                    objRound.IsCurrent = true;
                    acc.SaveOrUpdate(objRound);
                    acc.Commit();
                    RoomState = rooInfo.State;
                    RoundId = objRound.Id.ToString();
                }
            }
            catch(Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
            SetData();
            

            //Response.Redirect("RoomList.aspx");
        }

        protected void btnExce_Click(object sender, EventArgs e)
        {
            
            if (btnExce.Text == "结账")
            {
                var winExec = new Window(Page, "结算信息");
                winExec.Width = 360;
                winExec.Height = 480;
                winExec.ShowModalDialog("PayBill.aspx?RoomName=" + lblName.Text);
            }
            var acc = new CoreDataAccess();
            try
            {
                acc.BeginTransaction();
                var obj = acc.GetSingleObjectById(typeof(RoomInfo).Name, RoomId);
                if (obj != null && obj is RoomInfo)
                {
                    var rooInfo = ((RoomInfo)obj);
                    rooInfo.State = RoomInfo.STATE_USING;
                    acc.SaveOrUpdate(rooInfo);
                    var objRound = new RoundInfo();
                    objRound.Room = ((RoomInfo)obj);
                    objRound.StartDate = DateTime.Now;
                    objRound.IsCurrent = true;
                    acc.SaveOrUpdate(objRound);
                    acc.Commit();
                    RoomState = rooInfo.State;
                    RoundId = objRound.Id.ToString();
                }
            }
            catch (Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
            SetData();
            
        }

        protected void lblName_Click(object sender, EventArgs e)
        {
            if (btnExce.Text != "结账")
                return;
            var win = new Window(Page, "房间消费清单");
            win.Width = 800;
            win.Height = 560;

            var acc = new CoreDataAccess();
            try
            {
                var obj = acc.GetRoundInfo(RoomId == "" ? Guid.Empty : (new Guid(RoomId)));
                if (obj != null) RoundId = obj.Id.ToString();
            }
            finally
            {
                acc.CloseSession();
            }

            win.ShowModalDialog("ConsumeInfo.aspx?RoundId=" + RoundId + "&RoomName="+Server.UrlEncode(RoomName));
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var winEdit = new Window(Page, "房间信息明细");
            winEdit.Width = 420;
            winEdit.Height = 360;
            winEdit.ShowModalDialog("RoomInfoEdit.aspx?RoomName=" + lblName.Text);

        }
    }
}
